'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url agent/setting/kh/RebackPercent 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			type,
			huichuan,
			amountRanges,
			tfid
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		let request_id = vk.pubfn.getUniCloudRequestId();
		let look_log = await db.collection('vk-test').add({
			tips: "回传配置进行了修改日志云函数",
			request_id: request_id,
		})
		if (type == "get") {
			res.RebackPercent = await db.collection('a_config').where({
				name: "RebackPercent",
				user_id: userInfo._id
			}).get()

			//不存在数据时,自动添加一条
			if (res.RebackPercent.data.length == 0) {
				let adds = await db.collection('a_config').add({
					name: "RebackPercent",
					user_id: userInfo._id,
					firstorder: true,
					newuser: true,
					hour_backhaul: 24,
					amountRanges: [],
					lqdhcbs:0,

				})
			}
			return res;
		}

		if (tfid && type == "save") {
			let update = await db.collection('a_config').where({
				name: "RebackPercent",
				tfid: tfid
			}).update({
			    lqdhcbs:huichuan.lqdhcbs,
				newuser: huichuan.newuser || 0,
				firstorder: huichuan.firstorder || 0,
				douyin: huichuan.douyin * 1 || 0,
				amountRanges: amountRanges,
				last_time: new Date().getTime()
			})
			return res;
		}

		if (type == "save" && !tfid) {
			let update = await db.collection('a_config').where({
				name: "RebackPercent",
				user_id: userInfo._id
			}).update({
				lqdhcbs:huichuan.lqdhcbs,
				newuser: huichuan.newuser || 0,
				firstorder: huichuan.firstorder || 0,
				douyin: huichuan.douyin * 1 || 0,
				hour_backhaul: huichuan.hour_backhaul * 1 || 0,
				amountRanges: amountRanges

			})
		}

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}